import {getHelp, netGetCertification} from '../service/TaskService'
import {authenicon} from '../const/imgs'
import * as locations from "../const/locations";
export const NameSpace = 'AuthenModel';

function createAction(action, payload) {
    return {type: `${NameSpace}/${action}`, payload};
}

export const Actions = {
    asyncActionGetAuthenList(params) {
       
        return createAction('asyncGetAuthenList', params)
    },
    asyncActionGetHelp() {
        return createAction('asyncgetHelp')
    }
};
function actionLoading(loading) {
    return createAction('loading', loading);
}

function actionBaseList(base) {
    return createAction('base', base);
}

function actionImproveList(improve) {
    return createAction('improve', improve);
}

function actionPrice(price) {
    return createAction('price', price);
}

function actionHelp(help) {
    return createAction('help', help)

}

function dealData(arr,Base, improve, placeholder) {
    let data;
    let info = Number.parseInt(arr.id);
    switch (info) {
        case 1:
            // Base.push(data);
            break;
        case 2:
            data = {
                images: authenicon.SFZ,
                title: '身份认证',
                path: locations.IDENTITY,
                placeholder: placeholder,
            };
            Base.push(data);
            break;
        case 4:
            data = {
                images: authenicon.YYS,
                title: '运营商认证',
                placeholder: placeholder,
                path: locations.OPERATORS,
            };
            Base.push(data);
            break;
        case 7:
            data = {
                images: authenicon.XL,
                title: '学信认证',
                placeholder: placeholder,
                path: locations.XXA,
            };
            improve.push(data);
            break;
        case 8:
            data = {
                images: authenicon.YH,
                title: '央行认证',
                placeholder: placeholder,
                path: locations.YHA,
            };
            improve.push(data);
            break;
        case 5:
            data = {
                images: authenicon.TB,
                placeholder: placeholder,
                title: '淘宝认证',
                path: locations.TBA,
            };
            improve.push(data);
            break;
        case 11:
            data = {
                images: authenicon.GJJ,
                title: '公积金认证',
                placeholder: placeholder,
                path: locations.GJJA,
                show: false
            };
            improve.push(data);
            break;
        case 12:
            data = {
                images: authenicon.SB,
                title: '社保认证',
                placeholder: placeholder,
                path: locations.SBA,
                show: false
            };
            improve.push(data);
            break;
        default:
    }
    // console.log(Base,
    //     improve)
    return{
        Base,
        improve
    }
}

export default {
    namespace: NameSpace,
    state: {
        loading: false,
        stat: '',
        Base: [],
        Improve: [],
        price: '',
        help: []
    },
    reducers: {
        loading(state, {payload}) {
            return {
                ...state,
                loading: state.Base ? null : payload
            }
        },
        base(state, {payload}) {
            return {
                ...state,
                Base: payload
            }
        },
        improve(state, {payload}) {
            return {
                ...state,
                Improve: payload
            }
        },
        price(state, {payload}) {
            return {
                ...state,
                price: payload
            }
        },
        help(state, {payload}) {
            return {
                ...state,
                help: payload
            }
        }
    },

    asyncTasks: {
        asyncGetAuthenList({dispatch}) {
            dispatch(actionLoading(true));
            netGetCertification().then((info) => {
                dispatch(actionLoading(false));
                let Base = [];
                let improve = [];
                dispatch(actionPrice(info.info.price.amount));
                info.info.ident[0].forEach((c) => {
                    dealData(c, Base, improve, '已认证')
                });
                info.info.ident[1].forEach((c) => {
                    dealData(c, Base, improve, '未认证')
                });
                dispatch(actionBaseList(Base));
                dispatch(actionImproveList(improve));
            }).catch((err) => {
                dispatch(actionLoading(false));
                console.log(err)
            });
        },
        asyncgetHelp({dispatch}) {
            getHelp().then((res)=>{
                dispatch(actionHelp(res.info));
            }).catch((err)=>{
                console.log(err)
            })
        }
    }
}